<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="关键词"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="社会统一信用代码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.organizationRegNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人" align="center">
        <template slot-scope="{row}">
          <span>{{ row.legalName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人身份证号码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.legalIdentityCard }}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人电话号码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.legalMobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center">
        <template slot-scope="{row}">
          <span>{{ row.contactor }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="140px" style="width: 80%; margin-left:20px;">
       
        <el-form-item label="公司名称" prop="c_name">
          <el-input v-model="temp.c_name" type="text" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="社会统一信用代码" prop="organizationRegNo">
          <el-input v-model="temp.organizationRegNo" type="text" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="法人" prop="legalName">
          <el-input v-model="temp.legalName" type="text" placeholder="请输入法人" />
        </el-form-item>
        <el-form-item label="法人身份证号码" prop="legalIdentityCard">
          <el-input v-model="temp.legalIdentityCard" type="text" placeholder="请输入法人身份证号码" />
        </el-form-item>
        <el-form-item label="法人电话号码" prop="legalMobile">
          <el-input v-model="temp.legalMobile" type="text" placeholder="请输入法人电话号码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactor">
          <el-input v-model="temp.contactor" type="text" placeholder="请输入联系人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchCompanyList,
  createCompany,
  updateSaleStatus,
  resetPwd
} from '@/api/sale'
import {
  backendgetmerchantregion
} from '@/api/finance'
import { merchantListOnlyMerchant } from '@/api/merchant'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      company_list:[],
   
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: ''
      },
      saleListQuery:{
        page: 1,
        limit: 1000,
      },
      province_list: [],
    
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      sale_list:[],
      rules: {
        c_name: [{
          required: true,
          message: '公司名称必填',
          trigger: 'blur'
        }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList();
  },
  methods: {

    getList() {
      this.listLoading = true
      fetchCompanyList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.0 * 1000)
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {}
    },
    handleCreate() {
      this.resetTemp()
      this.dialogFormVisible = true
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createCompany(this.temp).then(() => {
            //this.list.unshift(this.temp)
            this.getList()
            // this.$refs['dataForm'].resetFields();
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },

    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
